import groovy.json.JsonOutput


/**
 *
 * 参考：
 * 玩Android的开发api
 * https://www.wanandroid.com/blog/show/2
 *
 * post formData:
 * https://github.com/Ablexq/MyGradle2/blob/master/pgyer/pgyer1.gradle
 */
class TestHttp {

    public static void main(String[] args) {


//        println "http://www.baidu.com/".toURL().text

//        println JsonOutput.prettyPrint("https://wanandroid.com/wxarticle/chapters/json".toURL().text)


//        HttpUtil.get("https://wanandroid.com/wxarticle/chapters/json")


//        registerTest()
//        loginTest()


//        HttpUtil.get("https://www.wanandroid.com/lg/collect/list/0/json")
//        HttpUtil.get("https://www.wanandroid.com/lg/collect/usertools/json")


//        HttpUtil.download("https://profile.csdnimg.cn/6/C/6/3_sinat_31057219")


        uploadPGY()
    }


    private static void uploadPGY() {
        def uploadPath = "https://www.pgyer.com/apiv2/app/upload"
        def filePath = "src\\file\\httphelperapk.apk"
        def pramMap = [
                "_api_key"              : "89d9dc80bb48639514dfd9480f5a592d",
                "uKey"                  : "9bdce1d8eb394872f59d1ae37af09348",
                "buildInstallType"      : "2",
                "buildUpdateDescription": "我是xq123456",
                "buildPassword"         : "123"
        ]
        def resp = HttpUtil.uploadPGY(uploadPath, filePath, pramMap)

        if (resp.code != 0) {
            throw new RuntimeException(resp.message)
        }

        //浏览器中打开短连接
        def url = "https://www.pgyer.com/" + resp.data.buildShortcutUrl
        println "url==================================$url"
    }

    private static void loginTest() {
        def loginUrl = "https://www.wanandroid.com/user/login"
        def loginPramMap = ["username": "xq1able",
                            "password": "pwd123456"]
        HttpUtil.postUrlEncode(loginUrl, loginPramMap)
    }

    private static void registerTest() {
        def registerUrl = "https://www.wanandroid.com/user/register"
        def registerParmMap = [:]
        registerParmMap["username"] = "xq1able"
        registerParmMap["password"] = "pwd123456"
        registerParmMap["repassword"] = "pwd123456"
        HttpUtil.postUrlEncode(registerUrl, registerParmMap)
    }

}
